<?php
namespace app\member\service;
/**
 * 财务处理
 */
class AccountService extends \app\base\service\BaseService {

    /**
     * 操作账户
     * @param $data
     * @return bool
     */
    public function account($data) {
        $data = [
            'user_id' => intval($data['user_id']),
            'money' => number_format($data['money'], 2),
            'pay_no' => html_clear($data['pay_no']),
            'pay_name' => html_clear($data['pay_name']),
            'type' => isset($data['type']) ? intval($data['type']) : 1,
            'deduct' => isset($data['deduct']) ? intval($data['deduct']) : 1,
            'remark' => html_clear($data['remark']),
        ];
        if(empty($data['user_id'])) {
            return $this->error('无法识别用户!');
        }
        if($data['money'] <= 0) {
            return $this->error('处理金额不正确!');
        }
        if(empty($data['pay_no']) || empty($data['pay_name'])) {
            return $this->error('支付号或支付名不正确!');
        }
        $model = target('member/PayAccount');
        $userInfo = $model->getWhereInfo([
            'A.user_id' => $data['user_id']
        ]);
        if(empty($userInfo)) {
            return $this->error('该账户不存在!');
        }
        //实际操作
        if($data['deduct']) {
            if(!$data['type']){
                if($userInfo['money'] < $data['money']){
                    return $this->error('账户余额不足,无法进行扣除!');
                }
            }
            $userData = array();
            if($data['type']){
                $userData['money'] = number_format(($userInfo['money'] + $data['money']), 2);
            }else{
                $userData['money'] = number_format(($userInfo['money'] - $data['money']), 2);
            }
        }
        if($data['type']){
            $userData['charge'] = number_format(($userInfo['charge'] + $data['money']), 2);
        }else{
            $userData['spend'] = number_format(($userInfo['spend'] + $data['money']), 2);
        }
        $userData['account_id'] = $userInfo['account_id'];
        if(!$model->edit($userData)){
            return $this->error('账户资金操作失败,请稍候再试!');
        }
        //写入记录
        $logData = array();
        $logData['account_id'] = $userInfo['account_id'];
        $logData['log_no'] = $this->logNo($userInfo['user_id']);
        $logData['time'] = time();
        $logData['money'] = $data['money'];
        $logData['remark'] = $data['remark'];
        $logData['pay_no'] = $data['pay_no'];
        $logData['pay_name'] = $data['pay_name'];
        $logData['type'] = $data['type'];
        $logId = target('member/PayLog')->add($logData);
        if(!$logId) {
            return $this->error('资金记录失败,请稍候再试!');
        }
        return $this->success($logId);
    }

    /**
     * 获取流水号
     * @param $userId
     * @return string
     */
    public function logNo($userId = ''){
        mt_srand((double) microtime() * 1000000);
        return $userId . date('Ymd') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
    }

    /**
     * 账户记录
     * @param $uid
     * @param $limit
     * @return mixed
     */
    public function accountLog($uid, $limit) {
        $where = array();
        $where['C.user_id'] = $uid;
        return target('member/PayLog')->loadList($where, $limit);
    }

    /**
     * 记录数量
     * @param $uid
     * @return mixed
     */
    public function AccountLogCount($uid) {
        $where = array();
        $where['C.user_id'] = $uid;
        return target('member/PayLog')->countList($where);
    }

    /**
     * 提现申请
     * @param $uid
     * @param string $money
     * @param string $account
     * @param string $name
     * @param string $type
     * @return bool
     */
    public function cash($uid, $money = '', $account = '', $name = '', $type = '') {
        $money = number_format($money, 2);
        $account = html_clear($account);
        $name = html_clear($name);
        $type = strtolower($type);
        if(empty($account) || empty($name) || empty($type)) {
            return $this->error('请输入或选择提现信息!');
        }
        if(empty($money)) {
            return $this->error('提现金额不正确!');
        }
        if($money < 50) {
            return $this->error('提现金额必须大于50元整!');
        }
        if($type <> 'alipay' && $type <> 'wechatpay') {
            return $this->error('提现类型不正确');
        }
        $model = target('member/PayAccount');
        $accountInfo = $model->getWhereInfo([
            'A.user_id' => $uid
        ]);
        if($accountInfo['money'] < $money) {
            return $this->error('账户余额不足,可用提现金额为'.$money);
        }
        $model->beginTransaction();
        $no = $this->logNo($uid);
        $status = $this->account([
            'user_id' => $uid,
            'money' => $money,
            'pay_no' => $no,
            'pay_name' => '提现',
            'type' => 0,
            'deduct' => 1,
            'remark' => '提现申请扣款',
        ]);
        if(!$status) {
            $model->rollBack();
            return false;
        }
        $data = array();
        $data['account_id'] = $accountInfo['account_id'];
        $data['cash_no'] = $no;
        $data['money'] = $money;
        $data['start_time'] = time();
        $data['account'] = $account;
        $data['account_type'] = $type;
        $data['account_name'] = $name;
        if(!target('member/PayCash')->add($data)){
            $model->rollBack();
            return $this->error(target('member/PayCash')->getError());
        }
        $model->commit();
        return $this->success();
    }

    /**
     * 提现记录
     * @param $uid
     * @param $limit
     * @return mixed
     */
    public function cashLog($uid, $limit) {
        $where = array();
        $where['C.user_id'] = $uid;
        return target('member/PayCash')->loadList($where, $limit);
    }

    /**
     * 提现记录
     * @param $uid
     * @return mixed
     */
    public function cashLogCount($uid) {
        $where = array();
        $where['C.user_id'] = $uid;
        return target('member/PayCash')->countList($where);
    }
}
